/**
 * 
 */
package main;

import java.io.Serializable;
import java.util.ArrayList;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsItem;
import com.trolltech.qt.gui.QGraphicsItemInterface;
import com.trolltech.qt.gui.QGraphicsLineItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QGraphicsSceneMouseEvent;
import com.trolltech.qt.gui.QLineF;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPen;
import com.trolltech.qt.gui.QStyleOptionGraphicsItem;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QGraphicsItem.GraphicsItemFlag;


/**
 * @author Guillaume
 *
 */
public abstract class QCustomArrow{
	protected QGraphicsClass _parent;
	protected QGraphicsClass _child;

	protected QGraphicsScene _scene;

	protected QPointF _start;
	protected QPointF _end;
	protected QPointF _middle;
	
	protected QGraphicsMiddlePoint middlePoint;
	protected boolean middleHasMoved;
	
	protected ArrayList<QGraphicsLineItem> lines;
	
	public QCustomArrow (QGraphicsClass parent, QGraphicsClass child, QGraphicsScene scene){
		_parent = parent;
		_child = child;
		_scene = scene;
		lines = new ArrayList<QGraphicsLineItem>();
		middleHasMoved=false;
		this.display();
	}

	public abstract void display();
	
	// Get the starting point of the arrow (i.e. not the arrowhead side)
	protected abstract QPointF getStartPoint();
	
	// Get the end point of the arrow (i. e. the tip of the arrowhead)
	protected abstract QPointF getEndPoint();
	
	// Calculate the coordinates of the middle point of the arrow
	protected QPointF calculateMiddlePoint(){
		return new QPointF((_start.x() + _end.x())/2, (_start.y()+_end.y())/2);
	}
	
	// draw a line between two points
	protected QGraphicsLineItem drawLine(QPointF start, QPointF end){
		QLineF herit = new QLineF(start, end);
		QGraphicsLineItem gHerit = new QGraphicsLineItem(herit, null , _scene);
		gHerit.setPen(new QPen(new QColor("black"), 1 ));
		gHerit.show();
		return gHerit;
	}
	
	public abstract void drawArrowHead(double px, double py, double cx, double cy);
	
	public boolean isValid(){
		return _parent.isVisible() && _child.isVisible();
	}
	
	public void dispose(){
		for(QGraphicsLineItem line : lines){
			line.dispose();
			middlePoint.dispose();
		}
	}
	
	protected class QGraphicsMiddlePoint extends QGraphicsItem implements Serializable{
		static final long serialVersionUID = 490867750339984306L;
		
		public QGraphicsMiddlePoint(QGraphicsItemInterface parent, QGraphicsScene scene){
			super(parent, scene);
		}
		
		@Override
		public QRectF boundingRect() {
			return new QRectF(0,0,8,8);
		}
		
		@Override
		public void mouseMoveEvent(QGraphicsSceneMouseEvent event){
			middleHasMoved=true;
			_middle = new QPointF(pos().x()+4,pos().y()+4);
			Actions.SceneRedef scene = (Actions.SceneRedef)this.scene();
			scene.recalcInheritance();
			super.mouseMoveEvent(event);
		}
		
		
		@Override
		public void paint(QPainter painter, QStyleOptionGraphicsItem option,
				QWidget widget) {
			painter.setPen(new QPen(new QColor("black"), 1));
			System.out.println("ici");
			painter.drawArc(boundingRect(), 0, 360*16);
		}
	
	}
	
}
